$(function () {
    layui.use(['table', 'form', 'laydate', 'element', 'layer', 'laydate', 'element'], function () {
        var layer = layui.layer, element = layui.element
        $('.login').on('click', function () {
            openLogin(layer)
        })
        // 邮箱登录
        $(document).on('click', '#login-email-btn', function () {
            loginEmail(layer)
        })
        // 获取验证码
        $(document).on('click', '.load-verification', function () {
            loadVerification(layer)
        })
        $(document).on('click', '#login-btn', function () {
            login(layer);
        })
    })
    // 鼠标悬停个人中心头像
    $('.login-user-icon').hover(function () {
        $('.user-login-panel').show();
    }, function () {
        $('.user-login-panel').hide(20);
    })
    // 获取验证码按钮状态
    $(document).on('blur', '#login-email', function () {
        verificationBtnState()
    })
})

/*打开登录模态框*/
function openLogin(layer) {
    layer.open({
        shade: [0.8, '#151b2d'],
        type: 1,
        anim: 1,
        area: ['30%', ''],
        content: $('#login-panel').html(),
        resize: false,
        move: false,
        title: false,
        yes: function (index, layero) {
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        },
        success: function () {
            setTimeout(function () {
                verificationBtnState() // 检查获取按钮是否可点
            }, 10)
        }
    });
}

/**
 * 邮箱登录
 * @param layer
 */
function loginEmail(layer) {
    var email = $.trim($('#login-email').val())
    var verification = $.trim($('#login-verification').val())
    var flag = true
    if (email === '' || email === null) {
        layer.tips('登录邮箱不能为空', '#login-email', {tips: [3, '#202d4a']});
        flag = false;
        return;
    }
    if (verification === '' || verification === null) {
        layer.tips('邮箱验证码不能为空', '#login-verification', {tips: [3, '#202d4a']});
        flag = false;
        return;
    }
    if (flag) {
        layer.msg('登录中...', {icon: 16, shade: 0.5});
        $.ajax({
            url: '/user/querySingle',
            type: 'post',
            dataType: 'json',
            data: {email: email, verification: verification},
            success: function (data) {
                if (data.code === '000002') {
                    layer.msg('邮箱或验证码不正确。', {icon: 5});
                } else if (data.code === '000000') {
                    location.reload();
                } else if (data.code === '000301') {
                    layer.msg('邮箱或验证码不正确。', {icon: 5});
                }
            }
        })
    }
}

/**
 * 获取验证码按钮的状态
 */
function verificationBtnState() {
    var email = $.trim($('#login-email').val())
    if (email === '' || null) {
        $('.login-form .load-verification').addClass('layui-btn-disabled')
        $('.login-form .load-verification').attr("disabled", true)
    } else {
        $('.login-form .load-verification').removeClass('layui-btn-disabled')
        $('.login-form .load-verification').attr("disabled", false)
    }
}

/**
 * 获取邮箱验证码
 * @param layer
 */
function loadVerification(layer) {
    var email = $.trim($('#login-email').val())
    // 验证邮箱格式
    var emialReg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
    var flag = true
    if (!emialReg.test(email)) {
        layer.tips('登录邮箱格式不正确', '#login-email', {tips: [3, '#202d4a']});
        flag = false
    }
    if (flag) {
        layer.msg('发送中，请稍后...', {icon: 16, shade: 0.5});
        $.ajax({
            url: '/user/getVerification',
            type: 'post',
            dataType: 'json',
            data: {email: email},
            success: function (response) {
                if (response.code === '000000') {
                    layer.msg('验证码发送成功...', {icon: 1, time: 1000});
                } else if (response.code === '000002') {
                    layer.tips('登录邮箱不正确', '#login-email', {tips: [3, '#202d4a']});
                }
            }
        })
    }
}

/*密码登录*/
function login(layer) {
    var phone = $.trim($('#login-phone').val());
    var password = $.trim($('#login-password').val());
    var flag = true
    if (phone === '' || phone === null) {
        layer.tips('登录手机号不能为空', '#login-phone', {tips: [3, '#202d4a']});
        flag = false;
        return;
    }
    if (password === '' || password === null) {
        layer.tips('登录密码不能为空', '#login-password', {tips: [3, '#202d4a']});
        flag = false;
        return;
    }
    if (flag) {
        layer.msg('登录中...', {icon: 16, shade: 0.5});
        $.ajax({
            url: '/user/querySingle',
            type: 'post',
            dataType: 'json',
            data: {phone: phone, password: password},
            success: function (data) {
                if (data.code === '000002') {
                    layer.msg('手机或密码不正确。', {icon: 5});
                } else if (data.code === '000000') {
                    location.reload();
                }
            }
        })
    }
}

/**
 * 注册用户
 * @param layer
 */
function register(layer) {
    layer.open({
        shade: [0.8, '#151b2d'],
        type: 1,
        anim: 1,
        area: ['30%', ''],
        content: $('#register-panel').html(),
        resize: false,
        move: false,
        title: false,
        yes: function (index, layero) {
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        },
        success: function () {
        }
    });
}

/*个人中心*/
function personCenter() {
    location.href = '/user/center'
}

/*个人主页*/
function personIndex() {
    location.href = '/user/index'
}

/*藏经大楼*/
function recordList() {
    location.href = "/records" // 访问拦截器
}

/*退出系统*/
function logout() {
    $.ajax({
        url: '/user/logout',
        type: 'post',
        dataType: 'json',
        success: function (data) {
            if (data.code === '000000') {
                location.reload();
            } else {
                console.log('退出系统异常')
            }
        }
    })
}
